<?php
defined('SYSPATH') OR die('No direct access allowed.');
/**
 * home.php
 * @author ranuandra
 *
 * 14 Agu 2011 15:20:06
 *
 * 
 */

class Controller_Home extends Kohana_Mapi
{
	public $template = 'login';
	public function action_index()
	{
/*			try 
			{
				$_POST['username'] = "coba";
				$_POST['password'] = "12345678";
				$_POST['password_confirm'] = "12345678";
				$_POST['email'] = "adi@email.com";
				
				
				
				$user = ORM::factory('user');
				
				$user->create_user($_POST, array(
			 										'username',
													'password',
			 										'email',
			  									));
		  		$login_role = new Model_Role(array('name' =>'login'));
				$user->add('roles',$login_role);
		  		$login_role = new Model_Role(array('name' =>'admin'));
				$user->add('roles',$login_role);
				
			}
			catch (ORM_Validation_Exception $e) {
				$errors = $e->errors('models');
				print_r($errors);
			}	*/	
	}
	
	public function action_login()
	{
		$this->auto_render = false;
	
		if(isset($_POST['username']) and isset($_POST['password']))
		{
			$user = Auth_ORM::instance();
			
			//echo $_POST['password'];
			//$status = Auth_ORM::instance()->login($_POST['username'], $_POST['password'], FALSE);
			//echo "dsadasd";
			//print_r($status);
			$status = Auth_ORM::instance()->login($_POST['username'], $_POST['password'], FALSE);

			if($status==1)
			{
				//Request::current()->redirect(URL::base()."report");
				$data = array('success' => 'true', 'message' => 'Login Sukses');
				echo json_encode($data);
			}
			else 
			{
				$data = array('success' => 'false', 'message' => 'Username atau password salah');
				echo json_encode($data);
			}
			
			//print_r($status);
			//exit;
			//exit;
				
		}		
	}
}